/*
 * @Author: fisher
 * @Date: 2022-12-16 22:45:48
 * @LastEditTime: 2024-05-30 14:31:40
 * @LastEditors: yuhaijun 406875809@163.com
 * @Description:
 * @FilePath: /vite-vue-ts-pc/vite.config.ts
 * 可以输入预定的版权声明、个性签名、空行等
 */
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import eslintPlugin from 'vite-plugin-eslint'
import DefineOptions from 'unplugin-vue-define-options/dist/vite'
import Components from 'unplugin-vue-components/vite'
import { NaiveUiResolver } from 'unplugin-vue-components/resolvers'
import vueJsx from '@vitejs/plugin-vue-jsx'
import { createHtmlPlugin } from 'vite-plugin-html'
import AutoImport from 'unplugin-auto-import/vite'
// import OptimizationPersist from 'vite-plugin-optimize-persist'
// import PkgConfig from 'vite-plugin-package-config'
// import { visualizer } from 'rollup-plugin-visualizer'
// import vueI18n from '@intlify/vite-plugin-vue-i18n'

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  console.log(mode)
  return {
    base:
      mode === 'production' ? loadEnv(mode, process.cwd()).VITE_CDN_URL : './',
    resolve: {
      alias: [
        { find: /^~/, replacement: '' },
        { find: '@', replacement: resolve(__dirname, './src') }
      ]
    },
    plugins: [
      vue(),
      vueJsx(),
      DefineOptions(),
      AutoImport({
        include: [
          /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
          /\.vue$/,
          /\.vue\?vue/, // .vue
          /\.md$/ // .md
        ],
        imports: [
          'vue',
          'vue-router',
          'vue-i18n',
          {
            'naive-ui': [
              'useDialog',
              'useMessage',
              'useNotification',
              'useLoadingBar'
            ]
          }
        ],
        dts: './auto-imports.d.ts',
        eslintrc: {
          enabled: false, // Default `false`
          filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`
          globalsPropValue: true // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
        }
      }),
      Components({
        resolvers: [NaiveUiResolver()]
      }),
      eslintPlugin({
        include: ['src/**/*.ts', 'src/**/*.vue', 'src/*.ts', 'src/*.vue'],
        cache: false
      }),
      createHtmlPlugin({
        minify: true,
        pages: [
          {
            entry: 'src/main.ts',
            filename: 'index.html',
            template: 'index.html',
            injectOptions: {
              data: {
                title: '后台管理系统'
              }
            }
          },
          {
            entry: 'src/login/main.ts',
            filename: 'login.html',
            template: 'login.html',
            injectOptions: {
              data: {
                title: '登录'
              }
            }
          }
        ]
      }),
      // PkgConfig(),
      // OptimizationPersist()
      // visualizer({
      //   open: true,
      //   gzipSize: true
      // })
      // vueI18n({
      //   include: resolve(__dirname, './src/i18n/**')
      // })
    ],
    build: {
      sourcemap: false,
      cssCodeSplit: true,
      assetsInlineLimit: 10240, // 静态资源少于10kb转base64
      minify: 'terser',
      terserOptions: {
        compress: {
          warnings: false,
          drop_debugger: true, // console
          drop_console: true, // 注释console
          pure_funcs: ['console.log'] // 移除console
        }
      },
      rollupOptions: {
        input: {
          main: resolve(__dirname, 'src/main.ts'),
          login: resolve(__dirname, 'src/login/main.ts')
        },
        plugins: [
          // rollup plugin
        ],
        output: {
          manualChunks(id) {
            if (id.includes('node_modules')) {
              return id.includes('naive') ? 'naive' : 'vendor'
            }
          }
        }
      }
    },
    server: {
      port: 3334,
      hmr: true,
      proxy: {
        '/api': {
          // 要访问的跨域的域名
          target: loadEnv(mode, process.cwd()).VITE_PROXY,
          // ws: true, // 是否启用websockets
          rewrite: (path) => path.replace(/^\/api/, ''),
          secure: false, // 使用的是http协议则设置为false，https协议则设置为true
          changOrigin: true
        }
      }
    },
    // 修复 vue-i18n composition api warning
    define: {
      __VUE_I18N_FULL_INSTALL__: true,
      __VUE_I18N_LEGACY_API__: false,
      __INTLIFY_PROD_DEVTOOLS__: false
    }
  }
})
